package com.cet.algorithm.哈希表;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: algorithm
 * @description: 两数之和
 * @author: 陈恩涛
 * @create: 2022-03-16 08:37
 **/
public class LC01 {

    public static int[] twoSum(int[] nums, int target) {
        // key 存储元素， value存储下标
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{i, map.get(target - nums[i])};
            }
            map.put(nums[i], i);
        }
        return new int[0];
    }
}
